<?php
// DB ========================================
function mov_get_db_count() {
	global $CONN ;
	$site_id = Site::getSiteID();
	//print 'Site: '. $site_id;
	$ret = $CONN->get_single("SELECT COUNT(*) FROM player_movies where site_id = $site_id");
	return $ret[0];
} // f u n c t i o n
function mov_get_by_range($range, $order = 'm_name') {
	global $CONN ;
	$site_id = Site::getSiteID();
	return $CONN->get_list("SELECT * FROM player_movies where site_id = $site_id
	ORDER BY $order ".format_range($range));
} // f u n c t i o n
function mov_get_by_name($name) {
	global $CONN ;
	$site_id = Site::getSiteID();
	//print 'Site: '. $site_id;
	return $CONN->get_single("SELECT * FROM player_movies WHERE m_file = '$name' and site_id = $site_id ");
} // f u n c t i o n
function mov_update($ef, $m_name, $m_thumb, $m_var, $m_txt, $replace_date) {
	$replace_date = 1;
	global $CONN ;
	$site_id = Site::getSiteID();
	$fsize = filesize(_APP.'/movies/'.$ef);
	if($replace_date) {
		$a1 = ',m_date';
		$a2 = ", NOW()";
	} // i f
	return $CONN->get_mod("REPLACE INTO player_movies (m_file, m_fsize, m_name, m_thumb, m_var, m_txt$a1, site_id) VALUES ('$ef', '$fsize', '$m_name', '$m_thumb', '$m_var', '$m_txt'$a2, '$site_id')");
} // f u n c t i o n
function mov_del($name) {
	$fname = _APP . '/movies/'.$name;
	if(file_exists($fname)) {
		unlink($fname);
	} // i f
	global $CONN ;
	$CONN->get_mod("DELETE FROM player_movies WHERE m_file = '$name'");
} // f u n c t i o n
function movlist_clear() {
	$allfiles = get_dir(_APP.'/movies', 3);
	$site_id = Site::getSiteID();
	for($i=0; $i<count($allfiles); $i++) {
		$allfiles[$i] = "'{$allfiles[$i]}'";
	} // f o r
	global $CONN ;
	$CONN->get_mod("DELETE FROM player_movies WHERE site_id=$site_id and m_file NOT IN(".implode(',',$allfiles).")");
} // f u n c t i o n
function movlist_xml() {
	global $CONN ;
	$site_id = Site::getSiteID();
	$ret = $CONN->get_list("SELECT * FROM player_movies where site_id = $site_id ORDER BY m_name") ;
	$str = '<?'.'xml version="1.0" encoding="utf-8"'.'?'.'>'._E;
	$str .= '<movielist>'. _E ;
	for($i=0; $i<count($ret); $i++) {
		$str .= _T.'<movie>'. _E ;
			$str .= _T._T.'<id>'.$ret[$i][0].'</id>'. _E ;
			$str .= _T._T.'<name>'.$ret[$i][1].'</name>'. _E ;
			$str .= _T._T.'<img>'.$ret[$i][2].'</img>'. _E ;
			$str .= _T._T.'<playervar>'.$ret[$i][3].'</playervar>'. _E ;
			$str .= _T._T.'<filename>'.$ret[$i][4].'</filename>'. _E ;
			$str .= _T._T.'<filesize>'.$ret[$i][5].'</filesize>'. _E ;
			$str .= _T._T.'<description>'.$ret[$i][6].'</description>'. _E ;
			$str .= _T._T.'<date>'.$ret[$i][7].'</date>'. _E ;
		$str .= _T.'</movie>'. _E ;
	} // f o r
	$str .= '</movielist>'. _E ;
	return $str;
} // f u n c t i o n
function movlist_sendxml($txt) {
	@set_time_limit(0);
	@set_magic_quotes_runtime(0);
	@header("Content-disposition: inline; filename=movie-list-".date('m-d-y').".xml");
	@header("Content-length: " . strlen($txt));
	@header("Content-Type: application/x-octet-stream");
	@header("Pragma: no-cache");
	@header("Expires: 0");
	echo $txt ;
	exit ;
} // f u n c t i o n
// SERVICE ========================================
function format_fsize($fsizebyte) {
	if ($fsizebyte < 1024) {
		$fsize = $fsizebyte.' bytes';
	}
	elseif ($fsizebyte >= 1024 && $fsizebyte < 1048576) {
		$fsizeraw = ($fsizebyte/1000);
		$fsizefin = round($fsizeraw, 2);
		$fsize = $fsizefin.' <FONT COLOR=#000000>kb</FONT>';
	}
	else {
		$fsizeraw = ($fsizebyte/1048576);
		$fsizefin = round($fsizeraw, 2);
		$fsize = $fsizefin.' <B>mb</B>';
	}
	return $fsize ;
} // f u n c t i o n
function tr($i) {
	return '<tr style="background-color: '.alternate($i,'#DADADA','#F0F0F0').';">' ._E;
} // f u n c t i o n
function get_dir($abspath, $t = 0) {
	$files = array();
	if ($handle = opendir($abspath)) {
		while (false !== ($file = readdir($handle))) {
			if($file != '.' && $file != '..' && !ereg('^\.', $file)) {
				switch($t) {
					case 0 :
						$files[] = $file;
						break;
					case 1:
						if(is_file($abspath.'/'.$file)) $files[] = $file;
						break;
					case 2:
						if(is_dir($abspath.'/'.$file)) $files[] = $file;
						break;
					case 3:
						if(is_file($abspath.'/'.$file)&&ereg('\.php$', $file)) $files[] = $file;
						break;
					case 4:
						if(is_file($abspath.'/'.$file)&&ereg('\.flv$', $file)) $files[] = $file;
						break;
				} // s w i t c h
			} //if
		} // while
		closedir($handle);
	}//if
	return $files;
} // f u n c t i o n
function &paginate($count,$name,$props=array()) {
	$pm = &new SimplePageMaker($count,$_REQUEST[$name]);
	$pm->set('requestname',$name);
	$pm->set('delimiter',' <span>&middot;</span> ');
	$pm->set('link_active','color: white; font-size: 4; font-weight: bold;');
	$pm->set('link_inactive','color: white; font-size: 4; text-decoration: none; font-weight: bold;');
	$pm->set('items',10);
	$pm->set('itemsperpage',16);
	if(!empty($props)) {
		foreach($props as $k=>$v) {
			$pm->set($k,$v);
		} // foreach
	} // if
	$pm->make();
	return $pm ;
} // f u n c t i o n
function format_range($range) {
	return empty($range) ? '' : " LIMIT {$range[0]}," . ($range[count($range)-1] - $range[0] + 1) ;
} // f u n c t i o n

function alternate($i,$first,$second) {
	return (((int)$i - (int)((int)$i/2)*2) == 0) ? $first : $second ;
} // f u n c t i o n

function name_32() {
	return md5(uniqid(rand(0,10000)));
} // f u n c t i o n

function name_x($x) {
	return substr(name_32(),0,$x);
} // f u n c t i o n
function file_cont($abspath) {
	return fread(fopen($abspath,'r'),filesize($abspath));
} // f u n c t i o n
function img($name, $alt = '', $prot = _HTTP) {
	$path = $prot . '/picture_library/themes/'. _THEME .'/'. $name ;
	$s = GetImageSize(_APP.'/picture_library/themes/'. _THEME .'/'. $name);
	return '<img src="'.$path.'" '.$s[3].' border="0" alt="'.$alt.'">' ;
} // f u n c t i o n

function IsSubscriber() {	$yes=false;

    if(Session::isLogin()) {
		$user_id=Session::getUserId();
		if(User::isUserSubscriptionActive($user_id)){
			$yes=true;
		}
		else if(User::isUserAdmin($user_id)){
			$yes=true;
		}
  	}
    return $yes;}
?>